// pages/me/me.js
var acid = "";
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: getApp().globalData.userInfo,
    activityTime: null,
    list1: null, // 排队中
    list2: null, // 排到啦
    list3: null, // 已领取
    list4: null, // 未排到
  },


  serverClick: function () {
    wx.navigateTo({
      url: './server',
    })
  },

  // 排队中问号点击
  questionClick: function (e) {
    getApp().globalData.webViewUrl = e.target.dataset.href;
    wx.navigateTo({
      url: '../webview/webview',
    })
  },

  // 帖子点击
  topicClick: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.id
    })
  },



  ///取消领取 
  cancelReceiveClick: function (e) {
    var that = this;
    var pdid = e.target.dataset.pdid;
    wx.request({
      url: getApp().globalData.my_cancel_url + pdid,
      success: function(res) {
        console.log(res)
        wx.showToast({
          title: '取消领取',
          icon: 'none',
        })
        that.myqueueList(2); // 刷新排到了接口
      },
    })
  },

  /// 确认领取
  sureReciiveClick: function (e) {
    var that = this;
    var pdid = e.target.dataset.pdid;
    wx.request({
      url: getApp().globalData.my_surereceive_url + pdid,
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '确认领取',
          icon: 'none',
        })
        that.myqueueList(2); // 刷新排到了接口
      },
    })
  },

  // 删除排队
  removeQueueClick: function (e) {
    var that = this;
    console.log(e)
    wx.showLoading()
    wx.request({
      url: getApp().globalData.my_remove_queue_url,
      data: {
        id: e.target.dataset.id
      },
      success (res) {
        console.log(res)
        that.myqueueList(4);
      },
      complete(){
        wx.hideLoading()
      }
    })

  },

  // banner图点击
  topImgClick: function (e) {
    console.log(e)
    app.globalData.webViewUrl = e.target.dataset.href;
    wx.navigateTo({
      url: '../webview/webview',
    })
  },

  /// 填写收获地址 也叫创建订单
  address: function () {
    var nogetNum = 0;
    let arr = this.data.list2;
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].status != 5) {
        nogetNum++;
      }
    }

    if (nogetNum > 0) {
      wx.showToast({
        title: '您有' + nogetNum + '件衣服没有确认',
        icon: 'none'
      })
      return 
    }

    var that = this;

    wx.showLoading()
    /// 获取最后一个活动
    wx.request({
      url: getApp().globalData.get_activity_last_url,
      success (res) {
        console.log(res);
        acid = res.data.data.current.id;
        that.createOrder(acid)
      },
      complete () {
        wx.hideLoading()
      }
    })

    
  },

  // 创建订单
  createOrder: function (acid) {
    console.log(acid)
    /// 创建订单
    wx.showLoading()
    wx.request({
      url: getApp().globalData.order_add_url,
      data: {
        userId: getApp().globalData.userInfo.userId,
        acid: acid,
      },
      success(res) {
        console.log(res)
        if (res.data.code == -1) {
          wx.showToast({
            title: '订单已超时',
            icon: 'none'
          })
          return
        }
        // 跳转到确认订单页面
        wx.navigateTo({
          url: '../sureget/sureget?acid=' + acid,
        })
      },
      complete () {
        wx.hideLoading()
      }
    })
  },


  queuebtnClick: function () {

    wx.switchTab({
      url: '../home/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    // 活动
    wx.request({
      url: getApp().globalData.current_url,
      success(res) {
        console.log(res);
        if (res.data.data.current == null || res.data.data.current == undefined) {
        } else {
          var leftTime = res.data.data.current.end_time - res.data.data.current.current;
          that.countDown(leftTime);
        }
      }
    })

    this.setData({
      userInfo: getApp().globalData.userInfo,
    })

  },

  myqueueList: function (status) {
    var that = this;
    wx.showLoading()
    wx.request({
      url: getApp().globalData.my_queue_url,
      data: {
        userId: getApp().globalData.userInfo.userId,
        status: status,
      },
      success: function (res) {
        console.log(status,res)
        if (status == 1) {
          that.setData({
            list1: res.data.data.list,
          })
        }
        if (status == 2) {
          that.setData({
            list2: res.data.data.list,
          })
        }
        if (status == 3) {
          that.setData({
            list3: res.data.data.list,
          })
        }
        if (status == 4) {
          that.setData({
            list4: res.data.data.list,
          })
        }
        
      },
      complete(){
        wx.hideLoading()
      }
    })
  },

  // 倒计时方法
  countDown: function (leftTime) {
    var that = this;

    var leftHours = Math.floor(leftTime / 60 / 60);
    if (leftHours <= 9) {
      leftHours = '0' + leftHours;
    }
    var leftMinutes = Math.floor(leftTime / 60 % 60);
    if (leftMinutes <= 9) {
      leftMinutes = '0' + leftMinutes;
    }
    var leftSeconds = Math.floor(leftTime % 60);
    if (leftSeconds <= 9) {
      leftSeconds = '0' + leftSeconds;
    }


    that.setData({
      activityTime: leftHours + ":" + leftMinutes + ":" + leftSeconds,
    })

    if (leftTime > 0) {
      setTimeout(function () {
        leftTime--;
        that.countDown(leftTime);
      }, 1000);
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    

    // 获取我的排队 排队中
    this.myqueueList(1);

    // 排到啦
    this.myqueueList(2);

    // 已领取
    this.myqueueList(3);

    // 未排到
    this.myqueueList(4);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    if (e.from == 'menu') { // 分享小程序

      return {
        title: '快来加入闲置会发光，做最环保的时髦精>>',
        path: '/pages/login/login',
        imageUrl: '/resources/share_miniprogram.png',
      }
    }
    return {
      title: '我正在32个女生衣柜里找新欢，就差你支持了>>',
      path: '/pages/login/login?shareUserId=' + getApp().globalData.userInfo.userId + '&acid=' + acid + '&tid=' + e.target.dataset.tid + '&nickname=' + getApp().globalData.userInfo.nickName,
      imageUrl: '/resources/share_supportme.png',
    }
  }
})
